<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/28
 * Time: 17:05
 */
namespace controller;
use core\bootstrap;
use \model\model;
use model\article;
use \model\test as t;

class test extends bootstrap {
    function index($n=9){
        $time1=microtime();
        for($i=1;$i<=$n;$i++){
            for ($j=1;$j<=$i;$j++){
                echo $j."*".$i."=".$i*$j;
                echo "  ";
            }
            echo "<br>";
        }
        $time2=microtime();
        echo $time2-$time1;
    }

    public function run(){
        $model=model::getobject();
        $smodel=model::getobject();
        //$result=$model->insertquery('article','title','文章四');
        $result=$model->mquery('article','title');
        foreach ($result as $row){
            print_r($row);
        }
    }

    public function test(){
        $article=article::getobject();
        $result=$article->selects("*")->tablename()->wheres()->all();
        $data=[];
        foreach ($result as $row){
            $data[]=$row;
        }
        $this->assign('data',$data);
        $this->display('test');
    }

    public function r(){
        //$model=model::getobject('mysql:host=127.0.0.1;dbname=test','root','root');
        $article=article::getobject();
        $result=$article->selects("*")->tablename()->wheres(['id'=>2])->all();
        foreach ($result as $row){
             print_r($row);
         }
    }

    public function del(){

        $article=article::getobject();
        $result=$article->delete($_POST);
        if(!$result){
            $data=[
                'code'=>200,
                'state'=>1,
                'msg'=>'删除失败'
            ];

        }else{
            $data=[
                'code'=>200,
                'state'=>0,
                'msg'=>'删除成功'
            ];
        }

       echo  json_encode($data);
    }


    public function add(){
        extract($_POST);
       $article=article::getobject();
       if(empty($title)){
           $data=[
               'code'=>200,
               'state'=>1,
               'msg'=>'添加失败'
           ];
           echo json_encode($data);die();
       }
       $result=$article->insertquery('article','title',$title);

        if(!$result){
            $data=[
                'code'=>200,
                'state'=>1,
                'msg'=>'添加失败'
            ];

        }else{
            $data=[
                'code'=>200,
                'state'=>0,
                'msg'=>'添加成功'
            ];
        }
        echo json_encode($data);

    }

}